﻿using NetEasyMusic.Lib.Details;
using NetEasyMusic.Lib.Models;

namespace NetEasyMusic.Lib.Data;

public class MV : DataBasic
{
    public MV(string cookie) : base(cookie)
    {
    }

    public MV()
    {
    }
    
    public MV(string url, bool isUse = false) : base(url, isUse)
    {
    }

    
    /// <summary>
    /// 获取MV信息
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<VideoDetailModel> Detail(string id)
    {
        var obj = await GetFormString($"/mv/detail?mvid={id}");
        if (obj == null) return new VideoDetailModel();
        var video = new VideoDetailModel
        {
            // 
            Artists = obj["data"]!["artists"]!.Select(x => new ArtistModel(x)).ToArray(),
            PlayCount = obj["data"]!["playCount"]!.ToObject<string>()!,
            Id = id,
            Name = obj["data"]!["name"]!.ToObject<string>()!,
            Brs = obj["data"]!["brs"]!.Select(x => x["br"]!.ToObject<string>()).ToArray()!,
            Desc = obj["data"]!["desc"]!.ToObject<string>()!
        };
        video.Urls = await GetUrls(id, video.Brs);
        return video;
    }

    public async Task<string[]> GetUrls(string id, string[] brs)
    {
        var list = new List<string>();
        foreach (var br in brs)
        {
            var obj = await GetFormString($"/mv/url?id={id}&r={br}");
            if(obj == null)continue;
            list.Add(obj["data"]!["url"]!.ToObject<string>()!);
        }

        return list.ToArray();
    }
}